﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace RatpConsole
{
    class WebFilter
    {
        // Test with : http://www.ratp.fr/horaires/fr/ratp/bus/prochains_passages/PP/B179/179_5206_5436/A
        public String[] filter(String s)
        {
            String[] arr = new String[4];
            String tablePattern = "<tbody>(.*)</tbody>";
            String cellsPattern = "<tr>.*<td>(.*)</td><td>(.*)</td>.*</tr>.*<tr class=\"even \">.*<td>(.*)</td><td>(.*)</td>.*</tr>";
            Match matchTable = Regex.Match(s, tablePattern, RegexOptions.Singleline);
            if (!matchTable.Success) return arr;
            Match matchCells = Regex.Match(matchTable.Value, cellsPattern, RegexOptions.Singleline);

            if (matchCells.Success && matchCells.Groups.Count == 5)
            {
                for (int i = 1; i < 5;i++)
                {
                    arr[i-1] = matchCells.Groups[i].Value;
                }
                return arr;
            }
            else
            {
                return arr;
            }
        }
    }
}
